﻿using System;
using System.Web;
using System.Web.SessionState;
using System.Collections.Generic;
using System.Text;
using System.Data;

using Gift.DAL;
using Gift.Model;
using Gift.WebApp;

namespace WebApp.cn.ashx
{
    public class pointsForOrderPayHandler : IHttpHandler, IRequiresSessionState
    {
        tbUser oUser = new tbUser();

        /// <summary>
        /// 您将需要在您网站的 web.config 文件中配置此处理程序，
        /// 并向 IIS 注册此处理程序，然后才能进行使用。有关详细信息，
        /// 请参见下面的链接: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpHandler Members

        public bool IsReusable
        {
            // 如果无法为其他请求重用托管处理程序，则返回 false。
            // 如果按请求保留某些状态信息，则通常这将为 false。
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {

            if (context.Request.Form["orderNo"] != null)
            {
                DataTable orderInfoByOrderNo = BaseFactory.thePointsForOrder.GetOrderInfoByOrderNo(context.Request.Form["orderNo"], (context.Session["user"] as tbUser).id);
                if (orderInfoByOrderNo.Rows.Count > 0)
                {
                    DataRow row = orderInfoByOrderNo.Rows[0];
                    DataRow row2 = BaseFactory.theUser.GetUserById((context.Session["user"] as tbUser).id).Rows[0];
                    if (Convert.ToInt32(row2["payPoints"]) >= Convert.ToInt32(row["orderIntegral"]))
                    {
                        this.oUser.id = Convert.ToInt32(row2["id"]);
                        this.oUser.money = Convert.ToDecimal(row2["money"]);
                        this.oUser.payPoints = Convert.ToInt32(row2["payPoints"]) - Convert.ToInt32(row["orderIntegral"]);
                        if (BaseFactory.theUser.ModifyUserMoneyAndPayPoints(this.oUser))
                        {
                            BaseFactory.thePointsForOrder.ModifyPayStatusByOrderNo(context.Request.Form["orderNo"], 1);
                            context.Response.Write("1");
                        }
                        else
                        {
                            context.Response.Write("0");
                        }
                    }
                    else
                    {
                        context.Response.Write("0");
                    }
                }
            }
        }

        #endregion
    }
}
